package com.dharma.mydiet.view;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dharma.mydiet.MainApplication;
import com.dharma.mydiet.R;
import com.dharma.mydiet.storage.TransactionalUser;
import com.dharma.mydiet.utility.Utils;
import com.google.analytics.tracking.android.EasyTracker;

public class HomeActivity extends Activity {

    private Button newConsume;
    private Button shootAlarm;
    private Button statistics;
    private Button myProfile;
    private Button infoFoods;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	// TODO Auto-generated method stub
	super.onCreate(savedInstanceState);
	this.setContentView(R.layout.activity_home);
	newConsume = (Button) findViewById(R.id.buttonNewConsume);
	Utils.applyCustomFont((TextView) newConsume, getApplicationContext());
	shootAlarm = (Button) findViewById(R.id.buttonShootAlarm);
	Utils.applyCustomFont((TextView) shootAlarm, getApplicationContext());
	statistics = (Button) findViewById(R.id.buttonStatistics);
	Utils.applyCustomFont((TextView) statistics, getApplicationContext());
	myProfile = (Button) findViewById(R.id.buttonMyInfo);
	Utils.applyCustomFont((TextView) myProfile, getApplicationContext());
	infoFoods = (Button) findViewById(R.id.buttonFoods);
	Utils.applyCustomFont((TextView) infoFoods, getApplicationContext());
	
	int title = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
	Utils.applyCustomFont((TextView) findViewById(title), getApplicationContext());

	newConsume.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(getApplicationContext(), NewRegisterActivity.class);
		startActivity(intent);
	    }
	});
	infoFoods.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(getApplicationContext(), InfoFoodsAcrtivity.class);
		startActivity(intent);
	    }
	});
	myProfile.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(getApplicationContext(), MyProfileActivity.class);
		startActivity(intent);
	    }
	});

	statistics.setOnClickListener(new OnClickListener() {
	    @Override
	    public void onClick(View v) {
		Intent intent = new Intent(getApplicationContext(), StatisticsActivity.class);
		startActivity(intent);
	    }
	});

	shootAlarm.setOnClickListener(new OnClickListener() {

	    @Override
	    public void onClick(View v) {
		TransactionalUser user = ((MainApplication) getApplication()).getCurrentUser();
		String phoneNo = user.getTelefonoUrgencias();
		String sms = "Estoy teniendo problemas en este momento.";

		try {
		    SmsManager smsManager = SmsManager.getDefault();
		    smsManager.sendTextMessage(phoneNo, null, sms, null, null);
		    Toast.makeText(getApplicationContext(), "SMS Sent!", Toast.LENGTH_LONG).show();
		} catch (Exception e) {
		    Toast.makeText(getApplicationContext(), "SMS faild, please try again later!", Toast.LENGTH_LONG)
			    .show();
		    e.printStackTrace();
		}

	    }
	});
    }

    @Override
    public void onStart() {
	super.onStart();
	EasyTracker.getInstance(this).activityStart(this); // Add this method.
    }

    @Override
    public void onStop() {
	super.onStop();
	EasyTracker.getInstance(this).activityStop(this); // Add this method.
    }
}
